﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using ElectronicsMarket.Models;
using ElectronicsMarket.InternalService;
using ElectronicsMarket.EMServices;

namespace ElectronicsMarket.Controllers
{
    public class AccountController : Controller
    {
        private ServicesReleaseSoapClient serviceRelease;
        public AccountController()
        {
            serviceRelease = new ServicesReleaseSoapClient();
        }

        public ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public ActionResult LogOn(LogOnModel model)
        {
            string userName = model.UserName;
            string password = model.Password;
            if (ModelState.IsValid)
            {
                CUSTOMER customer = serviceRelease.getUserByUsernameAndPassword(userName, password);
                if (customer.CUSTOMER_SYS_NAME == "khach")
                    ModelState.AddModelError("", "Tên người dùng hoặc mật khẩu được cung cấp là không chính xác.");
                else
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    Session.Add("Username", customer.CUSTOMER_FISRT_NAME);
                    Session.Add("UserId", customer.CUSTOMER_ID.ToString());
                    return RedirectToAction("Index", "Home");
                }
            }
            return View(model);
        }

        public ActionResult LoginGadget()
        {
            return View();
        }

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            Session.Clear();
            return RedirectToAction("Index", "Home");
        }

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                int newId = serviceRelease.getLastUserId() + 1;
                
                bool result = serviceRelease.addNewCustomer(newId, model.UserName, model.Password, model.Email, DateTime.Now.Date, model.FirstName, model.LastName, model.Address, model.PhoneNumber, model.Birthday, model.Sex);
                if (result == true)
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    return RedirectToAction("Register");
                }
            }
            return View(model);
        }                                                     

        public ActionResult EditCustomerInfo()
        {
            CUSTOMER customer = serviceRelease.getUserById(int.Parse(Session["UserId"].ToString()));
            return View(customer);
        }
    }
}
